<template>
	<view class="content">
		<view class="image-area">
			<!-- <image src="../../static/apxrmyy.png" mode="widthFix"></image> -->
		</view>
		<view class="register-area">
			<button type="primary" shape="circle" @tap="toWx" :disabled="!checked">
				<u-icon name="weixin-fill" color="white" size="45" label="微信用户一键登录" labelColor="white"
					label-size="30"></u-icon>
			</button>
		</view>
		<view class="according-area">
			<label for="is">
				<u-checkbox id="is" v-model="checked" shape="circle">我已阅读并同意</u-checkbox>
			</label>
			<view>
				<text @click="navigatArticle('userAgreement')">用户协议</text>及<text
					@click="navigatArticle('secretTactis')">隐私策略</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: false,
				title: 'Hello',
				form: {
					wx_code: ''
				},
				src: 'https://gitee.com/flygoa/markdown-img/raw/master/markdown/729abc9a-3844-4498-b041-71e7f3e1a52d.jpeg',
				nickname: ''
			}
		},
		methods: {
			toWx() {
				// let that=this
				// that.logout();
				let code = ''
				this.$request("auth", "login", {
					account: 'oPjtk1VuwmU_hK8TrdbvbqYLJGFc',
					password: 'e8dc8ccd5e5f9e3a54f07350ce8a2d3d'
				}).then(res => {
					console.log("登陆", res)
					if (res.code == 1) {
						// console.log("后台返回",res)
						this.$store.commit('setToken', {
							token: res.data.AccessToken,
							mark: res.data.LoginMark,
							expire: res.data.Expires
						});
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}
				})
				 return
				console.log('登录！')
				// 在需要实现微信公众号登录的地方调用 uni.login() 方法
				console.log("local")
				// 判断是否是微信内置浏览器
				const isWechat = () => {
					let ua = window.navigator.userAgent.toLowerCase();
					return ua.match(/MicroMessenger/i) == 'micromessenger';
				}
				if (isWechat) {
					// 授权回调wx48dc25faf5951741
					// var appid = 'wxa10d8cbc31fcd0e3';
					//fuxing
					var appid = 'wx02fc1c837863be6e';
					//珠山妇幼
					// var appid = 'wx2623dffafa67fadf';
					//获取路径（要在微信内置浏览器打开，或者是用你部署环境的链接打开）
					let local = window.location.href.split("#")[0];
					//console.log("local",local)
					// 拼接在微信时打开的url
					let url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" +
						encodeURIComponent(local) + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
					// 从url中截取code
					code = this.GetParam(window.location.href.split("#")[0], "code");
					console.log("code", local)
					if (!code) {
						window.location.href = url;
					} else {
						// 处理截取到的code
						this.$request("auth", "wxlogin", {
							Code: code
						}).then(res => {
							if (res.code == 1) {
								uni.showToast({
									title: '登陆成功',
									icon: 'none',
									duration: 2000
								});
								console.log("后台返回", res)
								this.$store.commit('setToken', {
									token: res.data.AccessToken,
									mark: res.data.LoginMark,
									expire: res.data.Expires
								});
								uni.navigateBack()
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								});
							}

							// this.$dialog.msg('登录成功', 500).then(function() {

						})

					}

				} else {
					console.log('请在微信客户端打开')
				}

			},

			GetParam(url, code) {

				url = url + "";

				let regstr = "/(\\?|\\&)" + code + "=([^\\&]+)/";

				let reg = eval(regstr);

				//eval可以将 regstr字符串转换为 正则表达式

				let result = url.match(reg);

				if (result && result[2]) {

					return result[2];

				}

			},

		},

	}
</script>

<style>
	@import 'login.css'
</style>